package m.jclecai.com.utils;

import android.content.Context;
import android.text.TextUtils;

import java.lang.ref.WeakReference;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by Song on 2016/9/8.
 */
public class ShareSDKUtils {

    /**
     * @param title 标题
     * @param desc 内容描述
     * @param imageUrl 图片路径
     * @param url 分享内容的链接
     * @param site 应用名称
     * @param siteUrl 应用的网页地址
     * @param titleUrl 分享内容标题的链接地址
     * @param shareType 分享内容类型
     */
    private  String mTitle;
    private  String mDesc;
    private  String mImageUrl;
    private  String mUrl;
    private  String mSite;
    private  String mSiteUrl;
    private  String mTitleUrl;
    private  int mShareType;
    //赋值ApplicationContext
    private Context mContext;
    private Platform platform;
    private Platform.ShareParams sp;
    //定义成WeakReference类型防止内存泄漏
    private WeakReference<ShareCallback> mShareCallback;
    private volatile static ShareSDKUtils mShareSDKUtils;

    private ShareSDKUtils(Context context,ShareCallback shareCallback) {
        mShareCallback = new WeakReference<>(shareCallback);
        this.mContext =  context;
    }

    public static ShareSDKUtils getInstance(Context context,ShareCallback shareCallback) {
        if(mShareSDKUtils == null) {
            synchronized (ShareSDKUtils.class) {
                if(mShareSDKUtils == null) {
                    mShareSDKUtils = new ShareSDKUtils(context,shareCallback);
                    return mShareSDKUtils;
                }
            }
        }
        return mShareSDKUtils;
    }

    /**
     * 分享
     * @param platformName 平台名称
     */
    private void toPlatform(String platformName) {

        if(SinaWeibo.NAME.equals(platformName)) {
            sp = new SinaWeibo.ShareParams();
            platform = ShareSDK.getPlatform(platformName);
            if(!platform.isClientValid()) {
                return;
            }
        }else if(Wechat.NAME.equals(platformName)) {
            sp = new Wechat.ShareParams();
            platform = ShareSDK.getPlatform(platformName);
        }else if(QQ.NAME.equals(platformName)) {
            sp = new QQ.ShareParams();
            platform = ShareSDK.getPlatform(platformName);
            if(!platform.isClientValid()) {
                return;
            }
        }else{
            sp = new WechatMoments.ShareParams();
            platform = ShareSDK.getPlatform(platformName);
            if(!platform.isClientValid()) {
                return;
            }
        }

        platform.setPlatformActionListener(new PlatformActionListener() {

            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                if(mShareCallback.get() != null) {
                    mShareCallback.get().shareSuccess(platform, i, hashMap);
                }
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                if(mShareCallback.get() != null) {
                    mShareCallback.get().shareError(platform, i, throwable);
                }
            }

            @Override
            public void onCancel(Platform platform, int i) {
                if(mShareCallback.get() != null) {
                    mShareCallback.get().shareCancel(platform, i);
                }
            }
        });
    }

    /**
     * 分享到微信
     */
    public void shareWeChat() {
        toPlatform(Wechat.NAME);
        initData();
        platform.share(sp);
    }

    /**
     * 分享到微博
     */
    public void shareSina() {
        toPlatform(SinaWeibo.NAME);
        initData();
        platform.share(sp);
    }

    /**
     * 分享到QQ
     */
    public void shareQQ() {
        toPlatform(QQ.NAME);
        initData();
        platform.share(sp);
    }

    /**
     * 分享到QQ
     */
    public void shareWechatMoments() {
        toPlatform(WechatMoments.NAME);
        initData();
        platform.share(sp);
    }

    /**
     * 实例化分享值
     * @param title
     * @param desc
     * @param imageUrl
     * @param url
     * @param site
     * @param siteUrl
     * @param titleUrl
     * @param shareType
     * @return
     */
    public ShareSDKUtils initShareData(String title,String desc,
                                     String imageUrl, String url,
                                     String site, String siteUrl,
                                     String titleUrl,int shareType) {
        mTitle = title;
        mDesc = desc.length() >= 140 ? desc.substring(0,120) : desc;
        mImageUrl = imageUrl;
        mUrl = url;
        mSite = site;
        mSiteUrl = siteUrl;
        mTitleUrl = titleUrl;
        mShareType = shareType;
        return mShareSDKUtils;
    }

    /**
     * 设置分享属性
     */
    private void initData() {
        if(!TextUtils.isEmpty(mTitle)) {
            sp.setTitle(mTitle);
        }
        if(!TextUtils.isEmpty(mDesc)) {
            sp.setText(mDesc);
        }
        if(!TextUtils.isEmpty(mImageUrl)) {
            sp.setImageUrl(mImageUrl);
        }
        if(!TextUtils.isEmpty(mUrl)) {
            sp.setUrl(mUrl);
        }
        if(!TextUtils.isEmpty(mSite)) {
            sp.setSite(mSite);
        }
        if(!TextUtils.isEmpty(mSiteUrl)) {
            sp.setSiteUrl(mSiteUrl);
        }
        if(!TextUtils.isEmpty(mTitleUrl)) {
            sp.setTitleUrl(mTitleUrl);
        }
        if(mShareType != 0) {
            sp.setShareType(mShareType);
        }

    }

    /**
     * 分享状态回调
     */
    public interface ShareCallback {
        void shareSuccess(Platform platform, int i, HashMap<String, Object> hashMap);
        void shareError(Platform platform, int i, Throwable throwable);
        void shareCancel(Platform platform, int i);
    }
}
